package com.westar.core.dao;

import com.westar.base.model.AccountLimit;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 账号错误记录类
 */
@Repository
public class AccountLimitDao extends BaseDao {
    /**
     * 查询限定次数
     * @param account
     * @return
     */
    public Integer countAccountLimit(String account) {
        StringBuffer sql = new StringBuffer();
        List<Object> args = new ArrayList<>();
        sql.append("select count(id)");
        sql.append("from  accountLimit a ");
        sql.append("where 1=1 ");
        this.addSqlWhere(account,sql,args,"\n and a.account=?");
        return this.queryInt(sql.toString(),args.toArray());
    }

    /**
     * 查询某账号的限定信息
     * @param account
     * @return
     */
    public AccountLimit getRecentAccountLimit(String account){
        StringBuffer sql = new StringBuffer();
        List<Object> args = new ArrayList<>();
        sql.append("select *  from ( ");
        sql.append("    select a.*,row_number() over ( order by a.id desc) as row_number ");
        sql.append("    from  accountLimit a ");
        sql.append("    where 1=1 and a.limitTime is not null ");
        this.addSqlWhere(account,sql,args,"\n and a.account=?");
        sql.append(" ) t where row_number>0  and row_number<= 1");
        return (AccountLimit) this.objectQuery(sql.toString(),args.toArray(),AccountLimit.class);
    }
    /**
     * 查询某账号的限定信息
     * @return
     */
    public List<AccountLimit> listRecentAccountLimit(){
        StringBuffer sql = new StringBuffer();
        List<Object> args = new ArrayList<>();
        sql.append("select a.* ");
        sql.append("from  accountLimit a ");
        sql.append("where 1=1 and a.limitTime is not null ");
        sql.append("order by a.id ");
        return this.listQuery(sql.toString(),args.toArray(),AccountLimit.class);
    }
}
